<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Menu;
use Input;

class SmenuController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //获取后台菜单数据
        $datas = Menu::where('status','1')->orderBy('sort','asc')->get()->toArray();
        //进行菜单操作
        $dmenus = [];
        foreach($datas as $k => $v) {
            //判断是否是顶级分类
            if($v['pid'] == 0){
                $dmenus[$k] = $v;
                $dmenus[$k]['child'] = Menu::where('pid',$v['id'])->get();
            }
        }
        return view('admin.smenu.index',compact('dmenus'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        /**
         * time 2017-12-06
         * editor phpstorm
         * author kowy
         * @param 查出顶级菜单
         */
        $mdatas = Menu::where(['pid'=>'0','status'=>'1'])->orderBy('sort','asc')->get();
        return view('admin.smenu.add',compact('mdatas'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $this -> validate($request,[
            'name' => 'required|max:255|unique:menus',
            'icon' => 'max:25',
            'sort'=> 'numeric|max:100',
        ],[
            'name.required' => '菜单名称需要填写',
            'name.max' => '菜单名称过长',
            'name.unique' => '菜单名字重复',
            'icon.max' =>'图标描过长,请填写正确的图标',
            'sort.max' => '排序字段不要超过100',
            'sort.numeric' => '排序请填写数字',
    ]);
        //进行数据过滤
        $name = trim(Input::get('name'));
        $icon = trim(Input::get('icon'));
        $pid  = Input::get('menu_pid');
        $sort = Input::get('sort');
        $url  = trim(Input::get('url'));

        $res = Menu::create(['name'=>$name,'icon'=>$icon,'pid'=>$pid,'sort'=>$sort,'status'=>'1','url'=>$url]);
        if($res)
        return back()->with(['info'=>'添加菜单成功']);
        return back()->with(['info'=>'添加菜单失败']);

    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //查询出数据
        $dmenu = Menu::where('id',$id) -> select('id','pid','name','url','icon','sort')->first();
        //查询出pid
        if($dmenu['pid'] == '0'){
            $pid_name = '顶级菜单';
        }else{
            $pid_name = Menu::where('id',$id)->value('name');
        }
        return view('admin.smenu.show',compact('dmenu','pid_name'));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //根据id获得数据
        $mdatas = Menu::where('id',$id)->select('id','name','url','pid','sort','icon')->first();
        $pdatas = Menu::where(['pid'=>'0','status'=>'1'])->orderBy('sort','asc')->get();
        return view('admin.smenu.edit',compact('mdatas','pdatas'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //进行验证
        $this -> validate($request,[
            'name' => 'required|max:255|unique:menus,name,'.$id,
            'icon' => 'max:25',
            'sort'=> 'numeric|max:100',
        ],[
            'name.required' => '菜单名称需要填写',
            'name.max' => '菜单名称过长',
            'name.unique' => '菜单名字重复',
            'icon.max' =>'图标描过长,请填写正确的图标',
            'sort.max' => '排序字段不要超过100',
            'sort.numeric' => '排序请填写数字',
        ]);

        $name = trim(Input::get('name'));
        $icon = trim(Input::get('icon'));
        $url  = trim(Input::get('url'));
        $sort = trim(Input::get('sort'));
        $pid  = trim(Input::get('menu_pid'));
        $rpid = trim(Input::get('pid'));
        //如果是二级菜单,则可以直接更新成顶级类；
        if($pid != '0'){
            //则判断当前菜单是否是顶级菜单,如果是顶级菜单,而且下面有子类，则不能修改。如果是顶级菜单，下面没有子类，则可以更新成为子类。
            //如果是顶级菜单/判断有没有子类
            if($rpid == '0'){
                $havechild = Menu::where('pid',$id)->first();
                if(!empty($havechild)){
                    return back() -> with(['info'=>'不可以修改次顶级类,因为下面有子类']);
                }
            }
        }
        $res = Menu::where('id',$id)->update(['name'=>$name,'icon'=>$icon,'url'=>$url,'sort'=>$sort,'pid'=>$pid]);
        if($res !== false)
        return back() -> with(['info'=>'更新成功']);
        return back() -> with(['info'=>'更新失败']);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //判断是否是顶级菜单,如果不是，可以直接删除；如果是，则先判断是否有子类；如果有子类，则不能删除。如果没有，则直接删除；
        $pid = Menu::where('id',$id)->value('pid');
        if($pid == '0'){
            $havechild = Menu::where('pid',$id) -> first();
            if(!empty($havechild)){
                return response() -> json(['status'=>'have', 'msg'=>'删除失败']);
            }else{
                $res = Menu::destroy($id);
                if($res)
                return response() -> json(['status'=>'success','msg'=>'删除成功']);
                return response() -> json(['status'=>'error',  'msg'=>'删除失败']);
            }

        }else{
            $res = Menu::destroy($id);
            if($res)
            return response() -> json(['status'=>'success','msg'=>'删除成功']);
            return response() -> json(['status'=>'error',  'msg'=>'删除失败']);
        }
    }
}
